package com.lyl.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.domain.SysUser;
import com.lyl.model.Result;
import com.lyl.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author lylbell
 * @date 2021-06-23 21:54
 */
@RestController
@Api(tags = "后台员工管理")
@RequestMapping("/users")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    @ApiOperation(value="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示条数"),
            @ApiImplicitParam(name = "mobile",value = "手机号码"),
            @ApiImplicitParam(name = "fullname",value = "员工姓名")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public Result<Page<SysUser>> findByPage(@ApiIgnore Page<SysUser> page, String mobile, String fullname) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysUser> userPage = sysUserService.findByPage(page,mobile,fullname);
        return Result.ok(userPage);
    }

    @PostMapping
    @ApiOperation(value="新增员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser",value = "员工的json数据")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public Result add(@RequestBody @Validated SysUser sysUser) {
        boolean save = sysUserService.saveUser(sysUser);
        if (save) {
            return Result.ok("员工添加成功");
        }
        return Result.fail("员工添加失败");
    }

    @PatchMapping
    @ApiOperation(value="修改员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser",value = "员工的json数据")
    })
    @PreAuthorize("hasAuthority('sys_user_update')")
    public Result update(@RequestBody @Validated SysUser sysUser) {
        Boolean update = sysUserService.updateUser(sysUser);
        if (update) {
            return Result.ok();
        }
        return Result.fail("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation(value="删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "要删除的员工")
    })
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public Result delete(@RequestBody String[] ids) {  //要加上@RequestBody不然会报错
        if (ids == null || ids.length == 0) {
            return Result.fail("请选择要删除的员工");
        }
        //删除员工，相应的要删除员工对应的角色（员工角色表）
        boolean delete = sysUserService.removeUsers(ids);
        if (delete) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户的id")
    })
    public Result<SysUser> getById(Long userId) {
        SysUser user = sysUserService.getById(userId);
        return Result.ok(user);
    }
}
